<?php

class users extends MX_Controller{
    function __construct() {
        parent::__construct();
        $this->load->model('user_model');
    }
    
    function index()
    {
        $flag = TRUE;
        if ($this->input->post('username')) {
            $username = $this->input->post('username');
            $pass = $this->input->post('password');            
            $pass_hash = md5($pass);

            if ($this->user_model->get($username,$pass_hash)) {
                $arr = array(
                    'login' => true,
                    'username' => $username
                );
                $this->session->set_userdata($arr);

                redirect('admin/categories/show');
            } else {
                $flag = FALSE;
            }
        }

        $data['flag'] = $flag;
        $this->load->view('user_login', $data);
    }
    
    function setting()
    {
        $flag = TRUE;
        $hide = TRUE;
        $user = $this->user_model->get_user_detail($this->session->userdata('username'));
        
        if ($this->input->post('username')) {
            $username = $this->input->post('username');
            $displayname = $this->input->post('displayname');
            $pass_old = $this->input->post('pass');
            $pass_old_hash = md5($pass_old);
            $pass = $this->input->post('pass_new');            
            $pass_hash = md5($pass);

            if ($pass_old_hash == $user->pass) {
                if($this->user_model->edit($username,$pass_hash,$displayname))
                {
                    $hide = FALSE;
                    $flag = TRUE;
                }
                else
                {
                    $hide = FALSE;
                    $flag = FALSE;
                }
            } else {
                $hide = FALSE;
                $flag = FALSE;
            }
        }
        
        

        $data['hide'] = $hide;
        $data['flag'] = $flag;        
        $data['user'] = $user;
        $data['view'] = 'user_setting';
        $this->load->view('back_end/template', $data);
    }
}
?>
